package com.xzzz.common.base.log;

import com.xzzz.common.base.BaseProperties;
import com.xzzz.common.base.log.pojo.DynamicLog;
import com.xzzz.common.base.pojo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态日志级别
 */
@Slf4j
@RestController
@RequestMapping("/sys/log/dynamic")
public class DynamicLogController {

    @Autowired
    private DynamicLogRepository repository;

    @Autowired
    private BaseProperties properties;

    /**
     * 保存日志的级别
     */
    @PostMapping("/save")
    public R<?> save(@Validated @RequestBody DynamicLog dynamicLog) {
        Long expire = System.currentTimeMillis() + properties.getLog().getDuration();
        DynamicLogRepository.LevelWrapper levelWrapper =
                new DynamicLogRepository.LevelWrapper(
                        dynamicLog.getPath(),
                        dynamicLog.getLevel(),
                        expire);
        repository.save(levelWrapper);
        return R.ok();
    }
}
